package com.hq.ims.data.service;

import com.hq.ims.data.dao.AppPushUserMapper;
import com.hq.ims.data.entity.AppPushContent;
import com.hq.ims.data.entity.AppPushUser;
import com.hq.ims.data.entity.User;
import com.hq.ims.data.service.other.AppMessagePushBusService;
import com.hq.mybatis.base.BaseService;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.StringUtils;
import lombok.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * jiang bai ping
 *
 * @Email 526666031@qq.com
 * @Detail APP推送用户服务实现类
 */
@Service
public class BaseAppPushUserService extends BaseService<AppPushUserMapper, AppPushUser> {

    @Resource
    private AppMessagePushBusService appMessagePushService;

    @Resource
    private BaseUserService baseUserService;

    @Resource
    private BaseAppPushContentService baseAppPushContentService;

    /**
     * @param appPushContentId app推送内容id
     * @param notSendUserIds   未发送的用户id
     * @return : void
     * <p>
     * <p>
     * 初始化app推送用户
     */
    @Transactional(rollbackFor = Exception.class)
    public void initData(@NonNull String appPushContentId, @NonNull List<String> notSendUserIds) {
        List<AppPushUser> appPushUsers = new ArrayList<>();

        notSendUserIds.forEach(notSendUserId -> {
            AppPushUser appPushUser = new AppPushUser();
            appPushUser.setAppPushContentId(appPushContentId);
            appPushUser.setUserId(notSendUserId);
            appPushUser.setIsSend(false);
            appPushUser.setPushId(StringUtils.EMPTY);

            appPushUsers.add(appPushUser);
        });

        this.saveBatch(appPushUsers);
    }

    /**
     * @param userId 用户id
     * @return : void
     * <p>
     * <p>
     * 重发消息
     */
    @Transactional(rollbackFor = Exception.class)
    public void reissueMessage(String userId) {
        //获取用户信息
        User user = baseUserService.getById(userId);
        if (user == null) {
            return;
        }
        if (StringUtils.isEmpty(user.getPushId())) {
            return;
        }
        //获取用户待发送消息
        List<AppPushUser> appPushUsers = this.lambdaQuery().eq(AppPushUser::getUserId, userId).eq(AppPushUser::getIsSend, false).list();
        if (ListUtils.isNotExistsEle(appPushUsers)) {
            return;
        }

        appPushUsers.forEach(appPushUser -> {
            //获取推送消息内容
            AppPushContent appPushContent = baseAppPushContentService.getById(appPushUser.getAppPushContentId());
            if (appPushContent == null) {
                return;
            }
            //重发消息
            appMessagePushService.push(appPushContent.getTitle(), appPushContent.getBody(), appPushContent.getExtras(), userId);

            //修改用户待发消息状态
            appPushUser.setIsSend(true);
            appPushUser.setSendDateTime(LocalDateTime.now());
            appPushUser.setPushId(user.getPushId());
        });

        //更新
        this.saveOrUpdateBatch(appPushUsers);
    }
}
